﻿#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
# - http://www.zhengzhong.net/
# - http://blog.zhengzhong.net/
#
# Created on 2008-12-19.
# $Id: main.py 306 2008-12-19 18:56:59Z heavyzheng $
#

"""Bootstrap for running a Django application on Google App Engine."""

# Standard Python imports.
import os
import sys
import logging


#---------------------------------------------------------------------------------------------------
# Adapting Django to Google App Engine
#---------------------------------------------------------------------------------------------------

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

# Must set this env var before importing any more of Django.
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

# Google App Engine imports.
import google.appengine.ext.webapp.util

# Django imports.
import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher


# Set the logging level on the root logger.
if os.environ.get("SERVER_NAME") == "localhost":
    logging.getLogger().setLevel(logging.DEBUG)
else:
    logging.getLogger().setLevel(logging.INFO)


def log_exception(*args, **kwds):
    logging.exception("Exception in request:")

# Log errors.
django.dispatch.dispatcher.connect( log_exception,
                                    django.core.signals.got_request_exception )

# Unregister the rollback event handler.
django.dispatch.dispatcher.disconnect( django.db._rollback_on_exception,
                                       django.core.signals.got_request_exception )


#---------------------------------------------------------------------------------------------------
# extending sys.path
#---------------------------------------------------------------------------------------------------

# Extend sys.path to include additional library directories.
# sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
# logging.info("Set sys.path to : %s" % str(sys.path))

#---------------------------------------------------------------------------------------------------
# main entry point
#---------------------------------------------------------------------------------------------------

def main():
    # Create a Django application for WSGI.
    app = django.core.handlers.wsgi.WSGIHandler()
    # Run the WSGI CGI handler with that application.
    google.appengine.ext.webapp.util.run_wsgi_app(app)


if __name__ == '__main__':
    main()




